#ifndef DATA_H
#define DATA_H

#define LOW_POWER_LISTENING

typedef struct {
	uint32_t time;
	uint16_t voltage;
	uint16_t temperature;
	uint16_t humidity;
	uint16_t lightPar;
	uint16_t lightTsr;
} DataEntry;

typedef nx_struct DataMessage {
	nx_uint8_t source;
	nx_uint32_t time;
	nx_uint16_t voltage;
	nx_uint16_t temperature;
	nx_uint16_t humidity;
	nx_uint16_t lightPar;
	nx_uint16_t lightTsr;
} DataMessage;
  
enum {
  AM_DATAMESSAGE = 6,
  SLEEP_INTERVAL = 300,
  
  NUMBER_OF_NODES = 4,
  SAMPLEWINDOW_SIZE = 5,
  
  STEP_MIN = 120000, // minimum sampling time
  STEP_MAX = 600000, // maximum sampling time
  
  SENSE_INTERVAL = 1000,
  LEARN_INTERVAL = 2*(5*SENSE_INTERVAL+STEP_MAX),
  REPORT_INTERVAL = 120000,
  
  THR_LIGHT = 8,
  THR_STEP = 10, // broadcast change only if it is larger than this value
  
  BASE_ADDR = 0,
  DATA_DEST = 1
};

void data_msg2entry(DataMessage *msg, DataEntry *e) {
	e->time = msg->time;
	e->voltage = msg->voltage;
	e->temperature = msg->temperature;
	e->humidity = msg->humidity;
	e->lightPar = msg->lightPar;
	e->lightTsr = msg->lightTsr;
}

void data_entry2msg(DataEntry *e, DataMessage *msg) {
	msg->time = e->time;
	msg->voltage = e->voltage;
	msg->temperature = e->temperature;
	msg->humidity = e->humidity;
	msg->lightPar = e->lightPar;
	msg->lightTsr = e->lightTsr;
}

void fill_mean(DataEntry* mean, DataEntry table[], uint8_t table_size) {
	
	uint8_t i;
	
	mean->voltage = 0;
	mean->temperature = 0;
	mean->humidity = 0;
	mean->lightPar = 0;
	mean->lightTsr = 0;
	
	for (i = 0; i < table_size; ++i) {
		mean->voltage += table[i].voltage;
		mean->temperature += table[i].temperature;
		mean->humidity += table[i].humidity;
		mean->lightPar += table[i].lightPar;
		mean->lightTsr += table[i].lightTsr;
	}
	
	mean->voltage /= table_size;
	mean->temperature /= table_size;
	mean->humidity /= table_size;
	mean->lightPar /= table_size;
	mean->lightTsr /= table_size;
}

#endif
